package zhuyun.code.photoviews

import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*
import zhuyun.code.photoviews.adapter.PhotoViewAdapter


class MainActivity : AppCompatActivity() {
    private val photosNum = 10
    private lateinit var photoViewAdapter: PhotoViewAdapter
    private var currentSelectPosition = Array<Int>(1){_->0}
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        photoViewAdapter = PhotoViewAdapter(
            this,
            currentSelectPosition
        )
        photoListView.adapter = photoViewAdapter
        photoViewAdapter.setCurrentSelectPosition(0)
        tvPrevious.setOnClickListener{

                currentSelectPosition[0]--
                if(currentSelectPosition[0] < 0){
                    currentSelectPosition[0] += 10
                }
                if(currentSelectPosition[0] <= 9) {

                } else{
                    currentSelectPosition[0] -= 10
                }
                photoViewAdapter.setCurrentSelectPosition(currentSelectPosition[0])
                val firstVisiblePosition = photoListView.firstVisiblePosition
                val lastVisiblePosition = photoListView.lastVisiblePosition
                if(currentSelectPosition[0] < firstVisiblePosition){
                    photoListView.setSelection(currentSelectPosition[0])
                } else if(currentSelectPosition[0] <= lastVisiblePosition){

                } else {
                    photoListView.setSelection(currentSelectPosition[0])
//                    val scrollY = getScrollY()
//                    val hidePartHeight = (lastVisiblePosition+1)*DisplayUtil.dip2px(this,100.0f)-(scrollY+DisplayUtil.dip2px(this,400.0f))
//                    photoListView.smoothScrollBy(
//                        DisplayUtil.dip2px(
//                            this,
//                            100.0f * (currentSelectPosition[0] - lastVisiblePosition)+300.0f
//                        )+hidePartHeight, 100
//                    )
                }



        }
        tvNext.setOnClickListener {
                currentSelectPosition[0]++
                if(currentSelectPosition[0] >= 18){
                    currentSelectPosition[0] -= 10
                }
                if(currentSelectPosition[0] >= 8){

                } else {
                    currentSelectPosition[0] += 10
                }
                photoViewAdapter.setCurrentSelectPosition(currentSelectPosition[0])
                val firstVisiblePosition = photoListView.firstVisiblePosition
                val lastVisiblePosition = photoListView.lastVisiblePosition
                if(currentSelectPosition[0] < firstVisiblePosition){
                    photoListView.setSelection(currentSelectPosition[0]-3)
                } else if(currentSelectPosition[0] <= lastVisiblePosition){

                } else {
                    val scrollY = getScrollY()
                    val hidePartHeight = (lastVisiblePosition+1)*DisplayUtil.dip2px(this,100.0f)-(scrollY+DisplayUtil.dip2px(this,400.0f))
                    photoListView.smoothScrollBy(
                        DisplayUtil.dip2px(
                            this,
                            100.0f * (currentSelectPosition[0] - lastVisiblePosition)
                        )+hidePartHeight, 100
                    )
                }



        }

        gotoNextActivity.setOnClickListener { startActivity(Intent(this,RecyclerViewActivity::class.java)) }


    }

    private fun getScrollY():Int{
        val c: View = photoListView.getChildAt(0) ?: return 0
        val firstVisiblePosition: Int = photoListView.firstVisiblePosition
        val top = c.top
        return -top + firstVisiblePosition * c.height
    }
}
